package com.black.common.mybatis.datascope;

import com.black.common.annotation.DataScope;
import com.black.common.auth.UserContextHolder;
import com.black.common.constant.ShareConstant;
import com.black.common.util.ComCodeUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @author linwenfeng
 */
public class MyDataScopeSqlHandler extends AbstractDataScopeSqlHandler {

    /**
     * 如果没有配置，则使用默认的数据权限方案
     */
    @Value("${share.auth.default-data-power:true}")
    boolean defaultDataPower;

    @Override
    public String getType() {
        return "default";
    }

    /**
     * 获取权限数据sql
     *
     * @param dataPower 数据权限注解
     * @return 权限数据sql
     */
    @Override
    public String getScopeSql(DataScope dataPower) {
        List<String> dataPowerCodeList = getDataPowerCode(dataPower);
        String dataPowerCode = ShareConstant.DataScope.ALL;
        if (dataPowerCodeList.contains(dataPower.scopeType() + ShareConstant.DataScope.ALL)) {
            dataPowerCode = ShareConstant.DataScope.ALL;
        } else if (dataPowerCodeList.contains(dataPower.scopeType() + ShareConstant.DataScope.MINE)) {
            dataPowerCode = ShareConstant.DataScope.MINE;
        } else if (dataPowerCodeList.contains(dataPower.scopeType() + ShareConstant.DataScope.DEPART)) {
            dataPowerCode = ShareConstant.DataScope.DEPART;
        }
        StringBuilder powerSqlBuilder = new StringBuilder();
        String tableAlias = dataPower.tableAlias();
        if (!StringUtils.isEmpty(tableAlias) && !tableAlias.endsWith(ShareConstant.Symbol.POINT)) {
            tableAlias = tableAlias + ShareConstant.Symbol.POINT;
        }

        if (Objects.requireNonNull(dataPowerCode).contains(ShareConstant.DataScope.MINE)) {
            powerSqlBuilder.append(tableAlias)
                    .append(dataPower.creatorField()).append(" = ").append(UserContextHolder.getUserId());
        } else if (Objects.requireNonNull(dataPowerCode).contains(ShareConstant.DataScope.DEPART)) {
            if (defaultDataPower) {
                String powerComCode = ComCodeUtil.getPowerComCode(Objects.requireNonNull(UserContextHolder.getComCode()));
                powerSqlBuilder.append(tableAlias)
                        .append(dataPower.powerField());
                if (powerComCode.length() == UserContextHolder.getComCode().length()) {
                    powerSqlBuilder.append(ShareConstant.Symbol.EQUALS).append(powerComCode);
                } else {
                    powerSqlBuilder.append(" like ").append(ShareConstant.Symbol.SINGLE_QUOTES).append(powerComCode)
                            .append(ShareConstant.Symbol.PERCENT).append(ShareConstant.Symbol.SINGLE_QUOTES);
                }
                return powerSqlBuilder.toString();
            }
            Set<String> powers = platformFeignClient.getUserDataPower().getResult();
            if (powers != null && powers.size() > 0) {
                powerSqlBuilder.append(dataPower.powerField()).append("in").append("(");
                int i = 0;
                for (String power : powers) {
                    if (i == powers.size() - 1) {
                        powerSqlBuilder.append(power).append(")");
                    } else {
                        powerSqlBuilder.append(power).append(ShareConstant.Symbol.COMMA);
                    }
                    i++;
                }
            }
        }
        return powerSqlBuilder.toString();
    }
}
